//
//  GSDKComplatform+User.m
//  GameSDK
//
//  Created by Johnny on 2017/7/28.
//  Copyright © 2017年 Johnny. All rights reserved.
//

#import "GSDKComplatform+User.h"
#import "GSDKUserViewController.h"
#import "GSDKCommonMethod.h"
#import "YYKit.h"
#import "NSUserDefaults+Extension.h"
#import "GSDKComplatformBase.h"
#import "NSUserDefaults+Extension.h"
#import "GSDKUserConfig.h"
#import "GSDKServicesManager.h"
@implementation GSDKComplatform (User)
- (void)login {
    [GSDKCOMPLATFORM hideSuspensionToolBar];
    
    GSDKCOMMONMETHOD.userVc = nil;
    GSDKCOMMONMETHOD.userVc = [GSDKUserViewController show];
    if ([NSUserDefaults getValueIn:ISLOGIN] != nil) {
        [GSDKCOMMONMETHOD.userVc setupAccountView:true];
    } else {
        [GSDKCOMMONMETHOD.userVc setupSwtichAccountView];
    }
    GSDKCOMMONMETHOD.userVc.cancelBlock = ^{
        GSDKCOMMONMETHOD.userVc = nil;
    };
}

- (void)logout {
    if (GSDKCOMMONMETHOD.accountModel == nil) {
        //没有登录
        GSDKAccountStatus * user = [[GSDKAccountStatus alloc] init];
        user.gSDKAccountStatus = GSDK_ACCOUNT_STATUS_LOGOUT;
        
        GSDKErrorStatus * error = [[GSDKErrorStatus alloc] init];
        error.gSDKErrorStatus = GSDK_ERROR_STATUS_NOLOGIN;
        GSDKMessageStatus * message = [[GSDKMessageStatus alloc] init];
        message.code = @"-1";
        message.msg = [NSString localizedStringForKey:@"GSDK_nologin_error" value:@"没有登录"];
        [[NSNotificationCenter defaultCenter] postNotificationName:GSDKUserDidFinishNotification object:nil userInfo:@{@"GSDKAccountStatus": user, @"GSDKErrorStatus": error, @"GSDKMessageStatus": message}];
    } else {
        [NSUserDefaults removeValueIn:ISLOGIN];
        GSDKCOMMONMETHOD.accountModel = nil;
        
        GSDKAccountStatus * user = [[GSDKAccountStatus alloc] init];
        user.gSDKAccountStatus = GSDK_ACCOUNT_STATUS_LOGOUT;
        GSDKMessageStatus * message = [[GSDKMessageStatus alloc] init];
        message.code = @"0";
        message.msg = [NSString localizedStringForKey:@"GSDK_logoutsuccess_text" value:@"注销成功"];
        [[NSNotificationCenter defaultCenter] postNotificationName:GSDKUserDidFinishNotification object:nil userInfo:@{@"GSDKAccountStatus": user, @"GSDKMessageStatus": message}];
        
        [[GSDKServicesManager new] oneTimeRequest:[[GSDKLogoutModel alloc] initWithToken:GSDKCOMMONMETHOD.GSDKToken] andCompletionBlock:^(id responseObject) {
            DEBUGMSG(@"responseObject = %@", responseObject);
        }];
    }
}  

- (void)registerAccount {
    [GSDKCOMPLATFORM hideSuspensionToolBar];
    
    GSDKCOMMONMETHOD.userVc = nil;
    GSDKCOMMONMETHOD.userVc = [GSDKUserViewController show];
    [GSDKCOMMONMETHOD.userVc setupEmailRegisteredView];
    GSDKCOMMONMETHOD.userVc.cancelBlock = ^{
        GSDKCOMMONMETHOD.userVc = nil;
    };
}

- (BOOL)isLogin {
    return (GSDKCOMMONMETHOD.accountModel != nil);
}

- (void)switchAccount {
    [self logout];
    [[NSNotificationCenter defaultCenter] postNotificationName:GSDKUserCancelNoticationName object:nil];
    [GSDKCOMPLATFORM hideSuspensionToolBar];
    
    GSDKCOMMONMETHOD.userVc = nil;
    GSDKCOMMONMETHOD.userVc = [GSDKUserViewController show];
    [GSDKCOMMONMETHOD.userVc setupSwtichAccountView];
    GSDKCOMMONMETHOD.userVc.cancelBlock = ^{
        GSDKCOMMONMETHOD.userVc = nil;
    };
}

- (NSString *)username {
    if (GSDKCOMMONMETHOD.accountModel != nil) {
        return GSDKCOMMONMETHOD.accountModel.userName;
    }
    return nil;
}

- (NSString *)uid {
    if (GSDKCOMMONMETHOD.accountModel != nil) {
        return GSDKCOMMONMETHOD.accountModel.uid;
    }
    return nil;
}

- (NSString *)userToken {
    if (GSDKCOMMONMETHOD.accountModel != nil) {
        return GSDKCOMMONMETHOD.accountModel.token;
    }
    return nil;
}

@end

@implementation GSDKAccountStatus
@end
